package com.lovo.cloud.controller;

import com.lovo.dto.TicketDto;
import com.lovo.dto.ResultVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 项目名: SpringCloud
 * 文件名: CinemaController
 * 作者: pujiacheng
 * 日期: 2020/11/30
 * 描述:
 */
@RestController
public class CinemaController {

    private static int i=10;
//    private static List<TicketDto> ticketDtoList = null;

//    static {
//        ticketDtoList = new ArrayList<>();
//        int i = 1;
//        while (i <= 10) {
//            TicketDto ticketDto = new TicketDto();
//            String code = Math.random() * 10000 + "";
//            ticketDto.setName("复仇者联盟");
//            ticketDto.setCode(code);
//            ticketDto.setPrice(50);
//            i++;
//        }
//    }

    @GetMapping("ticket/{name}/{num}")
    public synchronized Object getTicket(
            @PathVariable String name,
            @PathVariable Integer num) {
//        TicketDto ticketDto = null;
        ResultVO resultVO = new ResultVO();
        if (i <= 0) {
            return "无票";
        }
        for (int j = 0; j < num; j++) {
//            ticketDto = ticketDtoList.get(i);
//            resultVO.getList().add(ticketDto.getCode());
            Integer code = (int)(Math.random()*100000);
            resultVO.getList().add(code+"");
            i--;
        }
        resultVO.setTotalPrice(50 * num);
        i = i - num;
        System.out.println(resultVO);
        return resultVO;
    }

}
